package edu.sctu.yueding.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.utils.Log;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.fragment.CommunityFragment;
import edu.sctu.yueding.fragment.IndexFragment;
import edu.sctu.yueding.fragment.IndexFragmentV2;
import edu.sctu.yueding.fragment.PersonalCenterFragment;
import edu.sctu.yueding.utils.SystemBarTintManager;
import edu.sctu.yueding.utils.httputils.SharePreference;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private View btnsview;
    private TextView textViewindex;
    private ImageView imageViewindex;
    private TextView textViewcommunity;
    private ImageView imageViewcommunity;
    private TextView textViewmyinfo;
    private ImageView imageViewmyinfo;
    private static int flag = 1;

    private RelativeLayout indexRel;
    private RelativeLayout communityRel;
    private RelativeLayout myInfoRel;

    private List<Fragment> fragments = new ArrayList<>();

    // 定义一个变量，来标识是否退出
    private static boolean isExit = false;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarAlpha(0.0f);
        initFragment();
        initView();
    }


    /**
     * 按2次退出
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
            SharePreference.getInstance(this).removeOneCache(CacheConfig.CACHE_IS_CLICK_UPDATE_CANCEL);
            SharePreference.getInstance(this).removeOneCache(CacheConfig.CACHE_IS_UPDATE);

        } else {
            appManager.exit();
            SharePreference.getInstance(this).removeOneCache(CacheConfig.CACHE_IS_CLICK_UPDATE_CANCEL);
            SharePreference.getInstance(this).removeOneCache(CacheConfig.CACHE_IS_UPDATE);


        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private void initView() {
        btnsview = findViewById(R.id.fragment_home_liner);

        textViewindex = (TextView) findViewById(R.id.fragment_home_btns_index_text);
        imageViewindex = (ImageView) findViewById(R.id.fragment_home_btns_index);

        textViewcommunity = (TextView) findViewById(R.id.fragment_home_btns_community_text);
        imageViewcommunity = (ImageView) findViewById(R.id.fragment_home_btns_community);


        textViewmyinfo = (TextView) findViewById(R.id.fragment_home_btns_myinfo_text);
        imageViewmyinfo = (ImageView) findViewById(R.id.fragment_home_btns_myinfo);


        indexRel = (RelativeLayout) findViewById(R.id.fragment_home_index);
        communityRel = (RelativeLayout) findViewById(R.id.fragment_home_community);
        myInfoRel = (RelativeLayout) findViewById(R.id.fragment_home_myinfo);

        indexRel.setOnClickListener(this);
        communityRel.setOnClickListener(this);
        myInfoRel.setOnClickListener(this);

        //TODO 后期根据相关的权限在做相应的修改

        flag = getIntent().getIntExtra("flag", 1);
        setDefaultFragment(flag);
    }


    /**
     * 供Activity 跳转到 Fragment 使用
     *
     * @param flag = 1 表示首页 2 比表示社区 3 表示个人中心
     */
    public void setDefaultFragment(int flag) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (flag) {
            case 1:
                fragmentTransaction.replace(R.id.fragment_home, fragments.get(0));
                afterClick(0);
                break;
            case 2:
                fragmentTransaction.replace(R.id.fragment_home, fragments.get(1));
                afterClick(1);
                break;
            case 3:
                fragmentTransaction.replace(R.id.fragment_home, fragments.get(2));
                afterClick(2);
                break;
            default:
                fragmentTransaction.replace(R.id.fragment_home, fragments.get(0));
                afterClick(0);
                break;
        }
        fragmentTransaction.commit();
    }


    private void initFragment() {
        fragments.add(new IndexFragmentV2());
        fragments.add(new CommunityFragment());
        fragments.add(new PersonalCenterFragment());

    }

    protected void setDefaultFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_home, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.fragment_home_index:
                setDefaultFragment(fragments.get(0));
                afterClick(0);
                break;
            case R.id.fragment_home_community:
                setDefaultFragment(fragments.get(1));
                afterClick(1);
                break;
            case R.id.fragment_home_myinfo:
                setDefaultFragment(fragments.get(2));
                afterClick(2);
                break;
        }
    }

    /**
     * 改变 底部
     *
     * @param i
     */
    private void afterClick(int i) {
        switch (i) {
            case 0:
                textViewindex.setTextColor(getResources().getColor(R.color.hometext));
                imageViewindex.setImageResource(R.drawable.home_select);

                textViewcommunity.setTextColor(getResources().getColor(R.color.hometext_unclick));
                imageViewcommunity.setImageResource(R.drawable.community);

                textViewmyinfo.setTextColor(getResources().getColor(R.color.hometext_unclick));
                imageViewmyinfo.setImageResource(R.drawable.user);
                break;
            case 1:
                textViewindex.setTextColor(getResources().getColor(R.color.hometext_unclick));
                imageViewindex.setImageResource(R.drawable.home);

                textViewcommunity.setTextColor(getResources().getColor(R.color.hometext));
                imageViewcommunity.setImageResource(R.drawable.community_select);

                textViewmyinfo.setTextColor(getResources().getColor(R.color.hometext_unclick));
                imageViewmyinfo.setImageResource(R.drawable.user);
                break;
            case 2:
                textViewindex.setTextColor(getResources().getColor(R.color.hometext_unclick));
                imageViewindex.setImageResource(R.drawable.home);

                textViewcommunity.setTextColor(getResources().getColor(R.color.hometext_unclick));
                imageViewcommunity.setImageResource(R.drawable.community);

                textViewmyinfo.setTextColor(getResources().getColor(R.color.hometext));
                imageViewmyinfo.setImageResource(R.drawable.user_select);
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        Log.d("result", "onActivityResult");
    }
}